package com.tanhua.server.serivce;

import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.tanhua.dubbo.api.PeachblossomApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.mongo.Peachblossom;
import com.tanhua.model.mongo.PeachblossomCount;
import com.tanhua.model.vo.PeachblossomVo;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class PeachblossomService {
        @Autowired
        private FastFileStorageClient client;////从调度服务器获取，一个目标存储服务器，上传
        @Autowired
        private FdfsWebServer webServer;// 获取存储服务器的请求URL
        @DubboReference
        private PeachblossomApi peachblossomApi;
        @Autowired
        private RedisTemplate<String,String> redisTemplate;
        @DubboReference
        private UserInfoApi userInfoApi;

        /**
         * 上传语音
         * @param soundFile
         * @throws IOException
         */
        public void savePeachblossom(MultipartFile soundFile) throws IOException {
            String fileName = soundFile.getOriginalFilename();
            fileName=fileName.substring(fileName.lastIndexOf(".")+1);//获取视频格式
            //上传语音到fastDFS
            StorePath storePath = client.uploadFile(soundFile.getInputStream(), soundFile.getSize(), fileName, null);
            //获取语音地址构建对象并保存至数据库
            String peachblossomUrl = webServer.getWebServerUrl() + storePath.getFullPath();
            Peachblossom peachblossom=new Peachblossom();
            peachblossom.setDate(System.currentTimeMillis());
            peachblossom.setPickkup(0);
            peachblossom.setUserId(UserHolder.getUserId());
            peachblossom.setSoundUrl(peachblossomUrl);
            peachblossomApi.save(peachblossom);
        }
        //查询语音
        public PeachblossomVo findPeachblossom() {
            //查询当日日期
            String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
            //通过userId,还有当天日期查询出操作用户今日拾取语音数对象
            PeachblossomCount peachblossomCount= peachblossomApi.findCount(UserHolder.getUserId(),date);
            Integer count;
            ObjectId id = null;
            //若对象为空
            if (peachblossomCount==null){
             //说明这是操作用户第一次查询
                    count=1;
            }else{
                // //若对象不为空,获取相应的属性方便后续使用
                count=peachblossomCount.getCount();
                 id = peachblossomCount.getId();
            }
            //判断拾取次数是否已经到达10次
            if (count>=10){
                //到达十次,返回虚拟数据,防止前端页面出现null等
                PeachblossomVo vo=new PeachblossomVo();
                vo.setAvatar("https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/tanhua/avatar_14.jpg");
                vo.setNickname("次数已用完");
                vo.setAge(18);
                vo.setSoundUrl("");
                vo.setRemainingTimes(0);
                return vo;
            }
            //不到达十次
            //查询出来一条语音
            Peachblossom  peachblossom= peachblossomApi.find(UserHolder.getUserId());
            //若没有可以拾取的语音数据
            if (peachblossom==null){
               //直接返回空对象
             return new PeachblossomVo();
            }
            //若有数据,查询userInfo
            UserInfo userInfo = userInfoApi.select(peachblossom.getUserId());
            //构建vo对象并返回
            PeachblossomVo vo = PeachblossomVo.init(userInfo, peachblossom);
            vo.setRemainingTimes(10-count);
            System.err.println("vo的数据=>>"+vo);
            //添加或修改今日当前用户已拾取语音数量
            peachblossomApi.saveOrUpdateCount(UserHolder.getUserId(),date,count+1);
            //修改此条语音数据的拾取状态
            peachblossomApi.updatePickkup(peachblossom.getId());
            return vo;
    }
}
